﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Scores
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ScrScore : ContentPage
    {
        public ScrScore()
        {
            InitializeComponent(); BindData(); Bindgpa();
        }

        public async void BindData()
        {
            // 删除原有数据
            myListView.ItemsSource = null;

            // 清除数据
            myListView.ItemsSource = new List<CourseGrade>();


            // 读取JSON文件内容
            string jsonFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "kebaiodata") + "/myCourseGrade.json"; // 替换成您的JSON文件路径

            //如果文件不存在，弹出提示并返回
            if (!File.Exists(jsonFilePath))
            {
                await DisplayAlert("提示", "能不能先返回输账号密码获取？", "可以");

                return;
            }

            string jsonData = File.ReadAllText(jsonFilePath);


            // 将JSON数据转换为对象列表
            //courses = JsonConvert.DeserializeObject<List<CourseGrade>>(jsonData);
            List<CourseGrade> courseGrades = JsonConvert.DeserializeObject<List<CourseGrade>>(jsonData);

            //修改数据
            foreach (CourseGrade courseGrade in courseGrades)
            {
                string[] CourseNames = courseGrade.CourseName.Split(' ');
                // 获取从第二个元素到最后一个元素的子数组

                string[] selectedCourseNames = CourseNames.Skip(3).ToArray();

                // 使用string.Join将子数组拼接成一个字符串
                courseGrade.CourseName = string.Join(" ", selectedCourseNames);

            }

            // 添加数据
            myListView.ItemsSource = courseGrades;
            time.Text = "更新时间：" + MainPage.time;
        }

        public void Bindgpa()
        {
            string pageContent1 = Regex.Replace(File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "kebaiodata") + "/myAllScore.html"), @"[\r\t\n]", "");
            // 使用正则表达式匹配指定的 td 标签，允许包含换行和空格字符
            string pattern = @"<b>平均学分绩点：</b> </font></div></td><td><div align=""left""><font color=""#FF0000"">(.*?)</font></div></td></tr>";
            Match matche = Regex.Match(pageContent1, pattern, RegexOptions.Singleline);
            string value = matche.Groups[1].Value.Trim();
            gdp.Text = "平均学分绩点：" + value;
        }


        public class CourseGrade
        {
            public string CourseName { get; set; }
            public string AttendanceWeight { get; set; }
            public string AttendanceGrade { get; set; }
            public string TotalGrade { get; set; }
        }

    }



    public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Color.Default;

            string stringValue = value.ToString();
            if (stringValue == "无" || stringValue == "总成绩")
                // 如果值为 "无"，返回浅灰色
                return Color.FromHex("#444144");
            else
                return Color.FromHex("#4A93ED"); // 返回16进制蓝色
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}